#include<graphics.h>
#include<conio.h>

#define High 480						//the option windows
#define Width 640

int main()
{
	int i,j;							//the circleate
	int ball_x[4] = {0};				//many balls position
	int ball_y[4] = {0};
	int vx[4] = {0},vy[4] = {0};		//many balls speed

	int temp_1,temp_2;					//temp number

	for(i=0;i<4;i++)					//give the initial number
	{
		ball_x[i] = rand()%Width;		
		ball_y[i] = rand()%High;
	}
	for(i=0;i<4;i++)					
	{
		vx[i] = 1;
		vy[i] = 1;
	}

	//the picture High and Width
	initgraph(Width,High);

	while(1)
	{
		//begin the furnish many pictures
		BeginBatchDraw();

		for(i=0;i<4;i++)
		{
			for(j=0;j<4;j++)
			{
				if(i != j)						//relieve the 
				{
					int sepe;					//judge the bounce
					sepe = (ball_x[i]-ball_x[j])*(ball_x[i]-ball_x[j])
						+(ball_y[i]-ball_y[j])*(ball_y[i]-ball_y[j]);
					if(sepe<1200)
					{
						temp_1 = vx[i];			//touch and temp the each number
						vx[i] = vx[j];
						vx[j] = temp_1;

						temp_2 = vy[i];
						vy[i] = vy[j];
						vy[j] = temp_2;
					}
				}
			}

			ball_x[i] += vx[i];					//the ball should move by itself
			ball_y[i] += vy[i];

			if(ball_x[i] < 20 || ball_x[i] > 620)	//the horizon question
			{
				vx[i] = - vx[i];
			}
			if(ball_y[i] < 20 || ball_y[i] > 460)
			{
				vy[i] = - vy[i];
			}
		}


		for(i=0;i<4;i++)
		{
			//appearance of the ball
			setfillcolor(GREEN);
			fillcircle(ball_x[i],ball_y[i],20);
		}
		
		FlushBatchDraw();
		Sleep(10);

		for(i=0;i<4;i++)
		{
			//cover the circle with BLACK
			setcolor(BLACK);
			setfillcolor(BLACK);
			fillcircle(ball_x[i],ball_y[i],20);
		}


	}
	
	//the touch to end
	getch();
	EndBatchDraw();
	closegraph();
	return 0;
}